package com.boc.langxiang.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.boc.langxiang.R;
import com.boc.langxiang.entity.bean.ProductTypeBean;
import com.boc.langxiang.util.StringUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Li Guoxi on 2016/6/29.
 */
public class ProductTypePop {
    private Context mContext;

    private RecyclerView mRecyclerView;

    private BaseQuickAdapter<ProductTypeBean> mAdapter;

    private List<ProductTypeBean> list = new ArrayList<>();

    private PopupWindow mPopupWindow;

    private PopupWindow.OnDismissListener mOndismissListener;

    public ProductTypePop(Context context, List<ProductTypeBean> list) {
        this.mContext = context;
        this.list = list;
        View view = LayoutInflater.from(context).inflate(R.layout.product_type_popup_layout, null);
        init(view);
    }

    private void init(View view) {
        mAdapter = new BaseQuickAdapter<ProductTypeBean>(R.layout.product_type_item, list) {
            @Override
            protected void convert(BaseViewHolder helper, ProductTypeBean item) {
                String show = "";
                String title = item.getTitle();
                String content = item.getContent();
                if (!StringUtil.isEmpty(content)) {
                    show = title + "-" + content;
                } else {
                    show = title;
                }
                helper.setText(R.id.title_tv, show);
            }
        };
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter);

        mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    public void show(View view, final OnTypeSelectListener onTypeSelectListener) {
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ProductTypeBean bean = mAdapter.getData().get(position);
                onTypeSelectListener.onSelect(bean);
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.setOnDismissListener(mOndismissListener);
        mPopupWindow.showAsDropDown(view);
    }

    public void setOndismissListener(PopupWindow.OnDismissListener ondismissListener) {
        this.mOndismissListener = ondismissListener;
    }

    public interface OnTypeSelectListener {
        void onSelect(ProductTypeBean bean);
    }
}
